#define STB_IMAGE_IMPLEMENTATION
#include "../stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../stb_image_write.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "../stb_image_resize.h"

#include <stdint.h>  // 确保 uint8_t 定义
#include <stdio.h>
#include <string>


// 图像基本信息结构体 (兼容原有代码)
typedef struct {
    uint8_t* data;   // 像素数据指针 (RGBA格式)
    int width;       // 图像宽度
    int height;      // 图像高度
} FrameInfo;

// 扩展图像结构体 (支持跨度和通道信息)
typedef struct {
    int w;           // 宽度
    int h;           // 高度
    int c;           // 通道数 (固定为4/RGBA)
    int w_stride;    // 行跨度 (width * c)
    uint8_t* data;   // 像素数据
} MyImg;

typedef struct {
    float x0, y0, x1, y1;
} Box_t;

int fill_dim(uint8_t* data, int h, int w);
void read_img(FrameInfo& in, MyImg& img, const std::string& img_path);


// 读取图像并填充结构体
void read_img(FrameInfo& in, MyImg& img, const std::string& img_path) {
    printf("img_path===> %s\n", img_path.c_str());

    int ori_img_w, ori_img_h, comp;
    unsigned char* imagedata = stbi_load(img_path.c_str(), &ori_img_w, &ori_img_h, &comp, 4); // 强制加载为RGBA
    if (!imagedata) {
        fprintf(stderr, "Error loading image: %s\n", img_path.c_str());
        return;
    }

    // 填充FrameInfo
    in.data = imagedata;
    in.width = ori_img_w;
    in.height = ori_img_h;
    printf("input img==> %d %d \n", in.width, in.height);

    // 填充MyImg
    img.w = ori_img_w;
    img.h = ori_img_h;
    img.c = 4;                  // 固定RGBA
    img.w_stride = ori_img_w * 4;
    img.data = imagedata;

    // 调试输出
    printf("Image loaded: width=%d, height=%d, channels=%d\n", ori_img_w, ori_img_h, comp);
}

void crop_image(const MyImg& in_img, const Box_t& box, MyImg& out_img) {
    // 1. 边界检查（手动实现）
    int x0 = (int)box.x0;
    int y0 = (int)box.y0;
    int x1 = (int)box.x1;
    int y1 = (int)box.y1;

    // 确保坐标不越界
    x0 = (x0 < 0) ? 0 : (x0 >= in_img.w ? in_img.w - 1 : x0);
    y0 = (y0 < 0) ? 0 : (y0 >= in_img.h ? in_img.h - 1 : y0);
    x1 = (x1 < 0) ? 0 : (x1 >= in_img.w ? in_img.w - 1 : x1);
    y1 = (y1 < 0) ? 0 : (y1 >= in_img.h ? in_img.h - 1 : y1);

    // 确保 x1 > x0 且 y1 > y0
    if (x1 <= x0 || y1 <= y0) {
        out_img.w = out_img.h = out_img.c = 0;
        out_img.data = nullptr;
        printf("crop box error \n");
        return;
    }

    // 2. 计算裁剪区域
    int crop_w = x1 - x0;
    int crop_h = y1 - y0;
    out_img.w = crop_w;
    out_img.h = crop_h;
    out_img.c = in_img.c;
    out_img.w_stride = crop_w * in_img.c;
    out_img.data = (uint8_t*)malloc(crop_h * out_img.w_stride);

    if (!out_img.data) {
        fprintf(stderr, "Memory allocation failed for %d bytes\n", crop_h * out_img.w_stride);
        return;
    }
    
    // 3. 逐行复制像素
    for (int y = 0; y < crop_h; ++y) {
        for (int x = 0; x < crop_w; ++x) {
            int src_pos = (y0 + y) * in_img.w_stride + (x0 + x) * in_img.c;
            int dst_pos = y * out_img.w_stride + x * in_img.c;
            memcpy(out_img.data + dst_pos, in_img.data + src_pos, in_img.c);
        }
    }
}

// 工具类实现：填充Alpha通道为0
int fill_dim(uint8_t* data, int h, int w) {
    for (int i = 0; i < h * w; i++) {
        data[i * 4 + 3] = 0;  // 设置Alpha通道为0 (data[i*4+3]对应RGBA中的A)
    }
    return 0;
}

// 示例用法
int main() {
    FrameInfo input_frame;
    MyImg input_img, crop_img;
    std::string image_path = "input.png";

    read_img(input_frame, input_img, image_path);
    
    if (!input_img.data) {
        fprintf(stderr, "Failed to load input image\n");
        return -1;
    }

    Box_t box = {50, 100, 600, 300};
    crop_image(input_img, box, crop_img);
    printf("crop_img==> %d %d \n", crop_img.w, crop_img.h);

    if (crop_img.data) {
        stbi_write_png("cropped.png", crop_img.w, crop_img.h, crop_img.c, 
                      crop_img.data, crop_img.w_stride);
        stbi_image_free(crop_img.data);
    }

    stbi_image_free(input_img.data);
    return 0;
}